package week3.day19;

import java.util.Arrays;

/**
 * @Author fengzihao
 * @Date 2024-05-03 20:16
 */
public class SortCat {
    public static void main(String[] args) {
        Cat c = new Cat(1, "小黄", 100);
        Cat c1 = new Cat(2, "小黑", 200);
        Cat c2 = new Cat(2, "貉", 2000);
        Cat c3 = new Cat(2, "马云的猫", 10000);
        Cat c4 = new Cat(2, "云哥", 8000);
        Cat c5 = new Cat(3, "吉祥物", 800);
        Cat c6 = new Cat(2, "阿修罗", 9000);
        Cat c7 = new Cat(2, "云堇", 300);
        Cat c8 = new Cat(2, "咪咪", 400);
        Cat c9 = new Cat(8, "养乐多", 500);
        Cat[] cats = {c, c1, c2, c3, c4, c5, c6, c7, c8, c9};
        Arrays.sort(cats);
        for (Cat cat : cats) {
            System.out.println(cat);
        }
        System.out.println("=================================");
        Arrays.sort(cats,CatComparator::comparator);
        for (Cat cat : cats) {
            System.out.println(cat);
        }
    }
}
class Cat implements Comparable{

    int age;
    String name;
    double price;

    public Cat(int age, String name, double price) {
        this.age = age;
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        Cat cat = (Cat) o;
        return this.age-cat.age;
    }
}
class CatComparator{
    public static int comparator(Cat cat1,Cat cat2){
        int result = cat1.age-cat2.age;
        result = result != 0?result:cat1.name.length()-cat2.name.length();
        result = result!=0?result:subtract(cat1.price,cat2.price);
        return result;
    }
    public static int subtract(double a,double b){
        if (a==b)
            return 0;
        return a>b?-1:1;
    }
}
